package com.jblv.frame.user.service.bll.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jblv.frame.common.enums.ExceptionEnum;
import com.jblv.frame.common.exception.FrameException;
import com.jblv.frame.common.type.BooleanType;
import com.jblv.frame.common.type.StatusType;
import com.jblv.frame.common.utils.UUIDGenerator;
import com.jblv.frame.user.api.bll.ISysUserBLL;
import com.jblv.frame.user.api.entity.SysUser;
import com.jblv.frame.user.api.params.SysUserListParams;
import com.jblv.frame.user.api.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description 管理员业务逻辑类
 * @Author   yangtao
 * @Create   2020-04-21
 **/
@Component
@Service
public class SysUserBLLImpl implements ISysUserBLL {

    @Autowired
    private ISysUserService sysUserService;

    private static Logger logger = LoggerFactory.getLogger(SysUserBLLImpl.class);


    @Override
    public SysUser get(String id) {

//        String key = BaseCacheUtils.getBeanCacheKeyById(id);
        SysUser bean = null;//BaseCacheUtils.get(key);
        if (bean != null) {
            return bean;
        }
        EntityWrapper<SysUser> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        wrapper.eq("USER_ID",id);
        bean = sysUserService.selectOne(wrapper);
        if (bean != null) {
            logger.info("add cache");
            //addCache(bean);
        }

        return bean;
    }

    @Override
    public SysUser getUserByUserName(String userName) {
        SysUser bean = null;//BaseCacheUtils.get(key);
        EntityWrapper<SysUser> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        wrapper.eq("USER_NAME",userName);
        bean = sysUserService.selectOne(wrapper);
        if (bean != null) {
            logger.info("add cache");
            //addCache(bean);
        }

        return bean;
    }

    @Override
    public SysUser getUserByPhone(String mobile) {
        SysUser bean = null;//BaseCacheUtils.get(key);
        EntityWrapper<SysUser> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        wrapper.eq("MOBILE",mobile);
        bean = sysUserService.selectOne(wrapper);
        if (bean != null) {
            logger.info("add cache");
            //addCache(bean);
        }

        return bean;
    }

    @Override
    public String save(String creator, SysUser sysUser) {
        if (sysUser == null) {
            return null;
        }
        if (StringUtils.isEmpty(sysUser.getUserId())) {
            sysUser.setUserId(UUIDGenerator.getUUID());
        }

        sysUser.setCreator(creator);
        sysUser.setCreateTime(System.currentTimeMillis());
        sysUser.setDeleteFlag(BooleanType.FALSE.getCode());

        boolean save = false;
        try {
            save = sysUserService.insert(sysUser);
        }catch (Exception e){
            logger.error("插入错误:{}",e.getMessage());
        }
        if (save) {
            //addCache(sysUser);
            return sysUser.getUserId();
        }

        return null;
    }

    @Override
    public boolean update(String operator, SysUser sysUser) {
        SysUser sysUserCheck = get(sysUser.getUserId());
        if (sysUserCheck == null) {
            throw new FrameException(ExceptionEnum.PARAM_ERROR);
        }
        if (sysUser != null) {

            sysUser.setUpdater(operator);
            sysUser.setUpdateTime(System.currentTimeMillis());

            boolean flag = sysUserService.updateById(sysUser);
            if (flag) {
                //SysUser newBean = sysUserService.selectById(sysUser.getId());
                //updateCache(oldUserInfo, newBean);
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean updateStatusById(String userId, Integer status, String operator) {
        if (StringUtils.isEmpty(userId)) {
            throw new FrameException(ExceptionEnum.PARAM_ERROR);
        }
        SysUser sysUser = new SysUser();
        //TODO  后期再优化改动 status 只能是 枚举的数据
        if (status!=null) {
            sysUser.setUserId(userId);
            sysUser.setUpdater(operator);
            if(StatusType.DISABLE.getCode()==status){
                sysUser.setStatus(StatusType.DISABLE.getCode());
            }else{
                sysUser.setStatus(StatusType.ENABLE.getCode());
            }

            boolean flag = update(operator,sysUser);
            if (flag) {
                //SysUser newBean = sysUserService.selectById(sysUser.getId());
                //updateCache(oldUserInfo, newBean);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean remove(String operator, String id) {

        if (StringUtils.isEmpty(id)) {
            return false;
        }
        SysUser sysUser = get(id);
        if (sysUser != null) {
            SysUser sysUserUpdate = new SysUser();
            sysUserUpdate.setDeleteFlag(BooleanType.TRUE.getCode());
            sysUserUpdate.setUpdater(operator);
            sysUserUpdate.setUserId(id);
            Boolean flag = sysUserService.updateById(sysUserUpdate);
            if (flag) {
                //removeCache(bayonetInfo);
                return true;
            }
        }
        return false;
    }

    @Override
    public Page<SysUser> getList(SysUserListParams params) {
        EntityWrapper<SysUser> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        if (params.getCreateTimeBegin() != null) {
            wrapper.ge("CREATE_TIME", params.getCreateTimeBegin());
        }
        if (params.getCreateTimeEnd() != null) {
            wrapper.lt("CREATE_TIME", params.getCreateTimeEnd());
        }

        if (params.getStatus() != null) {
            wrapper.eq("STATUS", params.getStatus());
        }
        if (params.getSex() != null) {
            wrapper.eq("SEX", params.getSex());
        }


        wrapper.orderBy("CREATE_TIME", false);


        Page<SysUser> page = sysUserService.selectPage(new Page<SysUser>(params.getPage(), params.getLimit()), wrapper);
        return page;
    }


}
